package badugi;

/**
 *
 * @author Asia
 */
import java.net.*;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Client implements Runnable {

    public static JFrame MainWindow= new JFrame();
    private static JButton connect = new JButton(); 
                            
    private static JLabel Lport = new JLabel(); 
    public static JLabel Lhost = new JLabel();
    public static JLabel Lnumber = new JLabel();
    private static JLabel LinitialCash = new JLabel();
    
    public static JTextField TFport = new JTextField();
    public static JTextField TFhost = new JTextField();
    public static JTextField TFnumber = new JTextField();
    public static JTextField TFinitialCash = new JTextField();
    
    private static Socket client = null;
    public static PrintWriter out;
    public static BufferedReader in;
    private static boolean isOn;
    static BufferedReader line;
        
    public static String hostname;
    public static int port;
    public static int number;
    public static int initialCash;

	Client(String hostname, int port) {

		this.hostname = hostname;
		this.port = port;
	}

        Client(Socket SOCK) {
            throw new UnsupportedOperationException("Not supported yet."); 
        //To change body of generated methods, choose Tools | Templates.
        }

	public void listen() {
		String response = "";
		try {
			while (((response = in.readLine()) != null) && (isOn)) {
				System.out.println(response);
				if (response.indexOf("***Bye") != -1) {
					isOn = false;
					this.close();
				}

			}
		} catch (IOException e) {

		}
	}

	public void close() {
		try {
			in.close();
			out.close();
			client.close();
		} catch (IOException e) {
			System.out.println("Error while closing streams");
		}

	}

	public void run() {
		listen();
	}

	public void setup() {

		this.connect();
		System.out.println("Creating new listening thread");
                
		new Thread(new Client(hostname, port)).start();

		System.out.println("Testing connection");

		out.println("1111");

		if (client != null && out != null && in != null) {
			try {
				while (isOn) {
					out.println(line.readLine().trim());
				}
			} catch (IOException e) {
				System.out.println("Error reading this line");
				System.exit(0);
			}

		}

	}
	
	private static void connect(){
		
		System.out.println("Trying to connect to server ");

		try {
			client = new Socket(hostname, port);
			out = new PrintWriter(client.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
					client.getInputStream()));
			line = new BufferedReader(new InputStreamReader(System.in));
		} catch (UnknownHostException e) {
			System.out.println(hostname + "is not found");
			return;
		} catch (IOException e3) {
			System.out.println("Cannot connect");
			System.exit(0);
		}

		System.out.println("Connected");
		isOn = true;

		
	}

	public static void main(String args[]) {
            
                BuildMainWindow();
                Initialize();
            
            
		Scanner s = new Scanner(System.in);
		final int port;
		String hostname;
                final int number;
                final int initialCash;     

		try {
			System.out.println("Podaj port");
			port = s.nextInt();
			System.out.println("Podaj nazwe hosta");
			hostname = s.next();
                        System.out.println("Podaj liczbę graczy");
			number = s.nextInt();
			System.out.println("Podaj nazwe stan konta");
			initialCash = s.nextInt();
		} catch (InputMismatchException e) {
			s.close();
			return;
		}

		Client client = new Client(hostname, port);
		client.setup();
		s.close();

	}

        public void finalize() {
		out.println("Q");
		close();
	}
        
        public static void Initialize(){
            connect.setEnabled(true);
            MainWindow.dispose();
        }
    
        public static void BuildMainWindow(){
            
            MainWindow.setSize(450,00);
            MainWindow.setLocation(220,180);
            MainWindow.setResizable(false);
            ConfigureMainWindow();
            MainWindow_Action();
            MainWindow.setVisible(true);   
            MainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        public static void ConfigureMainWindow(){
            MainWindow.getContentPane().setLayout(null);
        
            connect.setText("connect");
            connect.setBounds(190,130,110,25);
            connect.setToolTipText("");
            MainWindow.getContentPane().add(connect);
        
            Lport.setText("add port");
            MainWindow.getContentPane().add(Lport);
            Lport.setBounds(10,10,60,20);
        
            TFport.requestFocus();
            MainWindow.getContentPane().add(TFport);
            TFport.setBounds(120,10,260,30);
        
            Lhost.setText("add host");
            MainWindow.getContentPane().add(Lhost);
            Lhost.setBounds(10,40,60,20);
        
            TFhost.requestFocus();
            MainWindow.getContentPane().add(TFhost);
            TFhost.setBounds(120,40,260,30);
        
            Lnumber.setText("add number");
            MainWindow.getContentPane().add(Lnumber);
            Lnumber.setBounds(10,70,100,20);
        
            TFnumber.requestFocus();
            MainWindow.getContentPane().add(TFnumber);
            TFnumber.setBounds(120,70,260,30);
        
            LinitialCash.setText("add initial cash");
            MainWindow.getContentPane().add(LinitialCash);
            LinitialCash.setBounds(10,100,100,20);
        
            TFinitialCash.requestFocus();
            MainWindow.getContentPane().add(TFinitialCash);
            TFinitialCash.setBounds(120,100,260,30);
        }
    
        public static void Login_Action(){
        
            connect.addActionListener(
                new java.awt.event.ActionListener(){
                    public void actionPerformed(ActionEvent evt){
                        Action_connect();
                    }
                }
            );
        }

    public static void Action_connect(){
        
        if(!TFport.getText().equals("")&& !TFhost.getText().equals("") && !TFinitialCash.getText().equals("") && !TFnumber.getText().equals(""))
        {
            try{
                number=Integer.parseInt(TFnumber.getText());
                port=Integer.parseInt(TFport.getText());
                initialCash=Integer.parseInt(TFinitialCash.getText());
                
                connect.setEnabled(true);
                connect();        
            }
            catch(NumberFormatException e){
                JOptionPane.showMessageDialog(null,"give proper number values");
            }               
        }
        else{
            JOptionPane.showMessageDialog(null,"give all the values");
        }        
    }
    
    public static void MainWindow_Action(){
        
        connect.addActionListener(
            new java.awt.event.ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    BadugiGUI.BuildBadugi();
                }
            }
        );
       
    }

}


